using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Sirenix.OdinInspector;
using UnityEngine;

namespace HyuFrameWork
{
    public class PersistentService : ServiceBase
    {
        private static string SaveFolder;
        protected override void OnInitialize()
        {
            base.OnInitialize();
            SaveFolder = Application.persistentDataPath + "/save_data";
            GlobalVariables.Instance.GameStatus = LoadFromJson<GameStatus>("GameStatus");
            if (GlobalVariables.Instance.GameStatus == null)
            {
                GlobalVariables.Instance.GameStatus = new GameStatus();
                GlobalVariables.Instance.SaveGameStatus();
            }
        }

        public static void SaveByJson(string saveFileName, object data)
        {
            if (!Directory.Exists(SaveFolder))
                Directory.CreateDirectory(SaveFolder);
            
            var path = SaveFolder+"/"+saveFileName;
            using (FileStream fileStream = File.Create(path))
            {
                var json = JsonUtility.ToJson(data);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(fileStream,json);
#if UNITY_EDITOR
                Debug.Log($"Susscessfully saved data to {path}.");
#endif
            }
            #region 直接转json 不转化为二进制

            /*var json = JsonUtility.ToJson(data);
            var path = SaveFolder+"/"+saveFileName;;

            try
            {     
                File.WriteAllText(path, json);

#if UNITY_EDITOR
                Debug.Log($"Susscessfully saved data to {path}.");
#endif
            }
            catch (System.Exception exception)
            {
#if UNITY_EDITOR
                Debug.LogError($"Failed to save data to {path}. \n{exception}");
#endif
            }*/

            #endregion
        }

        public static T LoadFromJson<T>(string saveFileName)
        {
            var path = SaveFolder+"/"+saveFileName;
            if (!File.Exists(path))
            {
                Debug.LogWarning($"Didn't have {saveFileName} file");
                return default;
            }

            using FileStream fileStream = File.Open(path,FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();
            return JsonUtility.FromJson<T>((string)formatter.Deserialize(fileStream));
            #region 直接转json 不转化为二进制

            /*var path = Path.Combine(Application.persistentDataPath, saveFileName);

            try
            {
                var json = File.ReadAllText(path);
                var data = JsonUtility.FromJson<T>(json);

                return data;
            }
            catch (System.Exception exception)
            {
#if UNITY_EDITOR
                Debug.LogError($"Failed to load data from {path}. \n{exception}");
#endif
                return default;
            }*/

            #endregion
        }
        
        public static void OverwriteFromJson(string saveFileName,object obj)
        {
            var path = SaveFolder+"/"+saveFileName;
            if (File.Exists(path))
            {
                Debug.LogWarning($"Didn't have {saveFileName} file");
            }

            using FileStream fileStream = File.Open(path,FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();
            JsonUtility.FromJsonOverwrite((string)formatter.Deserialize(fileStream),obj);
        }
        

        public static void DeleteSaveFile(string saveFileName)
        {
            var path = SaveFolder+"/"+saveFileName;

            try
            {
                File.Delete(path);
            }
            catch (System.Exception exception)
            {
#if UNITY_EDITOR
                Debug.LogError($"Failed to delete {path}. \n{exception}");
#endif
            }
        }
    }
}